using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MUSA
{
    enum ShowTankInfo
    {
        State,
        Position,
        HealthPoint,
        ID
    }

    class TankTextData
    {
        #region Variables

        public Vector3 tankPosition;
        public string currentTankSate;
        public ushort tankID;
        public Color teamColor;
        public int healthPoint;

        #endregion

        #region Constructor

        public TankTextData()
        {
            tankPosition = new Vector3();
            currentTankSate = "NULL";
            tankID = 999;
            teamColor = Color.Red;
            healthPoint = 50;
        }

        public TankTextData(Vector3 pPosition , string pState , ushort pID , Color pTeamColor , int pHealthPoint)
        {
            tankPosition = pPosition;
            currentTankSate = pState;
            tankID = pID;
            teamColor = pTeamColor;
            healthPoint = pHealthPoint;
        }

        #endregion
    }

    class Text
    {
        #region Member Variables

        SpriteBatch spriteBatch;
        SpriteFont spriteFont;

        Queue<string> TextBox;
        Dictionary<ushort , TankTextData> TankInfoDict;

        ShowTankInfo mShowTankInfo;

        Vector2 TextBoxPos;

        Vector2 StatusPos;

        //turn info drawing on and off
        bool IsDrawn;

        //File to write text info
        StreamWriter outputFile;
        #endregion Member Variables

        #region Constructor
        public Text()
        {
            IsDrawn = false;
            mShowTankInfo = ShowTankInfo.State;
        }

        public Text(bool show)
        {
            IsDrawn = show;
        }
        #endregion Constructor

        #region Public Functions
        //Call this in LoadGraphics
        public void LoadFont()
        {
            TextBox = new Queue<string>();
            string text = " ";

            IsDrawn = true;

            //fill up textbox with empty lines of text
            for (short i = 0; i < 4; i++)
            {
                TextBox.Enqueue(text);
            }

            TankInfoDict = new Dictionary<ushort , TankTextData>();

            spriteFont = MUSA.mContentManager.Load<SpriteFont>("Media\\Text");
            spriteBatch = new SpriteBatch(MUSA.mGraphicsDevice);

            // Find the center of the string
            //TextBoxPos = spriteFont.MeasureString(output) / 2;

            TextBoxPos = new Vector2();

            StatusPos = new Vector2();

            string filename = "C:/MUSAPROJECT";

            //Check to see if "C:/MUSAPROJECT"  DOES NOT exists, than CREATE
            if (!Directory.Exists(filename))
                Directory.CreateDirectory(filename);

            filename = filename + "/" + DateTime.Today.Month + "_" + DateTime.Today.Day + "_" + DateTime.Today.Year;

            //Check to see if "C:/MUSAPROJECT/MONTH_DAY_YEAR"  DOES NOT exists, than CREATE
            if (!Directory.Exists(filename))
                Directory.CreateDirectory(filename);

            filename = filename + "/" + DateTime.Now.TimeOfDay.Hours + "_" + DateTime.Now.TimeOfDay.Minutes + ".txt";

            //CREATE Text File, if already created than OPEN
            outputFile = File.CreateText(filename);
        }

        //add new text to the message box
        public void addText(string pText)
        {
            //get rid of the a line of text
            TextBox.Dequeue();

            //add a new line of text
            TextBox.Enqueue(pText);
        }

        //update the tank data
        public void updateTankStat(Vector3 pPosition , TankState pState , ushort pID , TeamColor pColor , int pHealthPoint)
        {
            //if the tank does not exist within TankInfo, then add him in
            if (TankInfoDict.ContainsKey(pID) == false)
            {
                TankInfoDict.Add(pID , null);
            }

            //Determine the Color of the Tank
            Color tColor;
            switch (pColor)
            {
                case TeamColor.BLUE:
                    tColor = Color.Blue;
                    break;
                case TeamColor.GREEN:
                    tColor = Color.Green;
                    break;
                case TeamColor.RED:
                    tColor = Color.Red;
                    break;
                case TeamColor.YELLOW:
                    tColor = Color.Yellow;
                    break;
                default:
                    tColor = Color.White;
                    break;
            }

            string currentTankState;
            if (pState == TankState.Defense)
            {
                currentTankState = "Defense";
            }
            else if (pState == TankState.Offense)
            {
                currentTankState = "Offense";
            }
            else if (pState == TankState.Scout)
            {
                currentTankState = "Scout";
            }
            else if (pState == TankState.Retreat)
            {
                currentTankState = "Retreat";
            }
            else if (pState == TankState.InGroup)
            {
                currentTankState = "InGroup";
            }
            else
            {
                currentTankState = "Unkown";
            }

            //create the Data
            //TankTextData nData = new TankTextData(pPosition , pState , pID , tColor);
            TankTextData nData = new TankTextData(pPosition , currentTankState , pID , tColor , pHealthPoint);

            //set the data to the DICTIONARY
            TankInfoDict[pID] = nData;
        }

        //switch it on or off
        public void toggleDraw()
        {
            if (IsDrawn == false)
                IsDrawn = true;
            else
                IsDrawn = false;
        }

        //specify whether it is on or off
        public void toggleDraw(bool toggle)
        {
            IsDrawn = toggle;
        }

        //specify which TankInfo to show
        public void ChangeTankInfo(ShowTankInfo pTankInfo)
        {
            mShowTankInfo = pTankInfo;
        }

        public void Draw()
        {
            if (IsDrawn == true)
            {
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend , SpriteSortMode.Deferred , SaveStateMode.SaveState);

                ///////////////////
                // Draw the TextBox

                string output = "Status: \n";
                string outputText;
                foreach (string text in TextBox)
                {
                    output = output + text + "\n";
                    outputText = DateTime.Now.TimeOfDay + " -- " + text;
                    //outputFile.WriteLine(outputText);
                }

                //Set the Text Box Position
                TextBoxPos.X = 15;
                TextBoxPos.Y = 10;

                //Draw the Text Box
                spriteBatch.DrawString(spriteFont , output , TextBoxPos , Color.LightBlue);

                // Draw the TextBox
                ///////////////////

                ///////////////////
                // Draw TankStatus

                string status = "null";
                foreach (TankTextData tank in TankInfoDict.Values)
                {
                    switch (mShowTankInfo)
                    {
                        case ShowTankInfo.Position:
                            {
                                status = tank.tankPosition.X.ToString() + " , " + tank.tankPosition.Y.ToString();
                                break;
                            }
                        case ShowTankInfo.State:
                            {
                                status = tank.currentTankSate;
                                break;
                            }
                        case ShowTankInfo.HealthPoint:
                            {
                                status = tank.healthPoint.ToString();
                                break;
                            }
                        case ShowTankInfo.ID:
                            {
                                status = tank.tankID.ToString();
                                break;
                            }
                        default:
                            {
                                status = "NOTHING";
                                break;
                            }
                    }
                    //Position is based off the the Position of the tank offset by the CameraWidth divide by half, than divide by the CameraWidth
                    //Than, it is multiplied by the CameraWidth, plus a buffer to shift it over a bit
                    StatusPos.X = ((tank.tankPosition.X + (MUSA.mCamera.CameraWidth / 2)) / (MUSA.mCamera.CameraWidth))
                        * MUSA.mGraphicsDevice.Viewport.Width + (MUSA.mGraphicsDevice.Viewport.Width * (float)0.05);
                    StatusPos.Y = ((tank.tankPosition.Y - (MUSA.mCamera.CameraHeight / 2)) / (-1 * MUSA.mCamera.CameraHeight))
                        * MUSA.mGraphicsDevice.Viewport.Height;

                    spriteBatch.DrawString(spriteFont , status , StatusPos , Color.White);
                }

                // Draw TankStatus
                ///////////////////

                spriteBatch.End();
            }
        }

        public void RemoveTank(ushort pID)
        {
            TankInfoDict.Remove(pID);
        }

        #endregion Public Functions
    }
}


